package tech.aistar.controller;

import tech.aistar.pojo.Phone;
import tech.aistar.pojo.PhoneType;
import tech.aistar.service.IPhoneService;
import tech.aistar.service.IPhoneTypeService;
import tech.aistar.service.impl.PhoneServiceImpl;
import tech.aistar.service.impl.PhoneTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Ray
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/9/7  9:13
 */
@WebServlet(urlPatterns = "/phone/insert")
public class PhoneInsertAction extends HttpServlet {
    IPhoneService iPhoneService = new PhoneServiceImpl();
    IPhoneTypeService phoneTypeService = new PhoneTypeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理中文乱码
        req.setCharacterEncoding("utf-8");

        List<PhoneType> phoneTypeList = phoneTypeService.findAll();
        req.setAttribute("phoneTypeList",phoneTypeList);
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String free = req.getParameter("free");
        String intro = req.getParameter("intro");
        String type_id = req.getParameter("type_id");
        String url = req.getParameter("url");
//        String status = req.getParameter("status");
        if (name==null||price==null||free==null||intro==null||type_id==null||url==null){
            req.getRequestDispatcher("/prj/save/save.jsp").forward(req,resp);
        }else {
            Phone p = new Phone();
            p.setName(name);
            p.setPrice(Double.valueOf(price));
            p.setFree(Integer.valueOf(free));
            p.setIntro(intro);
            p.setTypeId(Integer.valueOf(type_id));
            p.setUrl(url);
            p.setStatus(0);
            iPhoneService.insert(p);
            req.getRequestDispatcher("/phone/list").forward(req,resp);
        }


    }
}
